/*
 * This file is part of aion-engine <aion-engine.net>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.gameobjects.player.emotion;

import com.aionemu.commons.database.dao.DAOManager;
import com.aionengine.gameserver.configs.main.MembershipConfig;
import com.aionengine.gameserver.dao.PlayerEmotionListDAO;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_EMOTION_LIST;
import com.aionengine.gameserver.taskmanager.tasks.ExpireTimerTask;
import com.aionengine.gameserver.utils.PacketSendUtility;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


/**
 * @author MrPoke
 * @modify xXMashUpXx
 */
public class EmotionList {
    private Map<Integer, Emotion> emotions;
    private Player owner;

    /**
     * @param owner
     */
    public EmotionList(Player owner) {
        this.owner = owner;
    }

    public void add(int emotionId, int dispearTime, boolean isNew) {
        if (emotions == null) {
            emotions = new HashMap<Integer, Emotion>();
        }
        Emotion emotion = new Emotion(emotionId, dispearTime);
        emotions.put(emotionId, emotion);

        if (isNew) {
            if (emotion.getExpireTime() != 0)
                ExpireTimerTask.getInstance().addTask(emotion, owner);
            DAOManager.getDAO(PlayerEmotionListDAO.class).insertEmotion(owner, emotion);
            PacketSendUtility.sendPacket(owner, new SM_EMOTION_LIST((byte) 1, Collections.singletonList(emotion)));
        }
    }

    public void remove(int emotionId) {
        emotions.remove(emotionId);
        DAOManager.getDAO(PlayerEmotionListDAO.class).deleteEmotion(owner.getObjectId(), emotionId);
        PacketSendUtility.sendPacket(owner, new SM_EMOTION_LIST((byte) 0, getEmotions()));
    }

    public boolean contains(int emotionId) {
        if (emotions == null)
            return false;
        return emotions.containsKey(emotionId);
    }

    public boolean canUse(int emotionId) {
        return emotionId < 64 || emotionId > 145 || (emotions != null && emotions.containsKey(emotionId)) || owner.havePermission(MembershipConfig.EMOTIONS_ALL);
    }

    public Collection<Emotion> getEmotions() {
        if (emotions == null)
            return Collections.emptyList();
        return emotions.values();
    }
}
